home *** CD-ROM | disk | FTP | other *** search
Text File | 1997-04-25 | 14.6 KB | 524 lines | [TEXT/CWIE] |
- // ==================================================
- // CTouchMeAppleEvents.cp
- // a part of CTouchMeApp.cp
- // Copyright (C) 1996-1997 Mizutori Tetsuya
- // July 4, 1996; February 3, 1997; April 24, 1997.
- // ==================================================
- // All documents are pretty-printed in 10-point Geneva font.
-
-
- #include <UAppleEventsMgr.h>
- #include <UExtractFromAEDesc.h>
-
- #include "touchMeConstants.h"
- #include "CTouchMeAppleEvents.h"
- #include "CTouchMeApp.h"
- #include "CTouchMePref.h"
- #include "CTouchMeMainWindow.h"
-
- #include "UAppleEvents.h"
- #include "UFileTools.h"
- #include "UMacOSTools.h"
-
-
- #define longDateTimeHi( a ) (((LongDateCvt *) &(a))->hl.lHigh)
- #define longDateTimeLo( a ) (((LongDateCvt *) &(a))->hl.lLow)
-
-
- // --------------------------------------------------
- // • HandleAppleEvent
- // --------------------------------------------------
- // Respond to an AppleEvent
-
- void
- CTouchMeApp::HandleAppleEvent(
- const AppleEvent & inAppleEvent,
- AppleEvent & outAEReply,
- AEDesc & outResult,
- long inAENumber )
- {
-
- switch ( inAENumber ) {
- case ae_GetPrefs:
- HandleGetPrefs( inAppleEvent, outAEReply, outResult );
- break;
-
- case ae_SetPrefs:
- HandleSetPrefs( inAppleEvent, outAEReply, outResult );
- break;
-
- case ae_LoadPrefs:
- HandleLoadPrefs( inAppleEvent, outAEReply, outResult );
- break;
-
- case ae_SavePrefs:
- HandleSavePrefs( inAppleEvent, outAEReply, outResult );
- break;
-
- case ae_Touch:
- HandleTouch( inAppleEvent, outAEReply, outResult );
- break;
-
- case ae_Fetch:
- HandleFetch( inAppleEvent, outAEReply, outResult );
- break;
-
- default:
- LDocApplication::HandleAppleEvent(
- inAppleEvent, outAEReply, outResult, inAENumber );
- break;
- }
- }
-
-
- // --------------------------------------------------
- // • HandleGetPrefs
- // --------------------------------------------------
-
- void
- CTouchMeApp::HandleGetPrefs(
- const AppleEvent & inAppleEvent,
- AppleEvent & outAEReply,
- AEDesc & outResult )
- {
- #pragma unused ( outAEReply )
-
- // Get the 1st required parameter of 'keyDirectObject' as 'typeEnumerated'.
- OSType theEnumValue;
- UAppleEvents::GetParamEnum( inAppleEvent, keyDirectObject, theEnumValue );
-
- // Error if there are more parameters.
- UAppleEventsMgr::CheckForMissedParams( inAppleEvent );
-
- ETouchType theTouchType;
- switch ( (EnumTouchMe_Type) theEnumValue ) {
- case kEnumType_Creation:
- theTouchType = touchType_CreationDate;
- break;
- case kEnumType_Modification:
- theTouchType = touchType_ModificationDate;
- break;
- default:
- ThrowIfOSErr_( errAEWrongDataType );
- break;
- }
-
- HandleGetPrefsSettings( outResult, theTouchType );
-
- }
-
-
- // --------------------------------------------------
- // • HandleSetPrefs
- // --------------------------------------------------
-
- void
- CTouchMeApp::HandleSetPrefs(
- const AppleEvent & inAppleEvent,
- AppleEvent & outAEReply,
- AEDesc & outResult )
- {
- #pragma unused ( outAEReply, outResult )
-
-
- // Get the 1st required parameter of 'keyDirectObject' as 'typeEnumerated'.
- OSType theEnumValue;
- UAppleEvents::GetParamEnum( inAppleEvent, keyDirectObject, theEnumValue );
-
- // Get the 2nd required parameter of 'keyAEData' as 'typeWildCard'.
- StAEDescriptor theDescData;
- // theDescData.GetParamDesc( inAppleEvent, keyAEData, typeAERecord );
- theDescData.GetParamDesc( inAppleEvent, keyAEData, typeWildCard );
-
- // Error if there are more parameters.
- UAppleEventsMgr::CheckForMissedParams( inAppleEvent );
-
- ETouchType theTouchType;
- switch ( (EnumTouchMe_Type) theEnumValue ) {
- case kEnumType_Creation:
- theTouchType = touchType_CreationDate;
- break;
- case kEnumType_Modification:
- theTouchType = touchType_ModificationDate;
- break;
- default:
- ThrowIfOSErr_( errAEWrongDataType );
- break;
- }
-
- HandleSetPrefsSettings( theDescData.mDesc, theTouchType );
-
- }
-
-
- // --------------------------------------------------
- // • HandleLoadPrefs
- // --------------------------------------------------
-
- void
- CTouchMeApp::HandleLoadPrefs(
- const AppleEvent & inAppleEvent,
- AppleEvent & outAEReply,
- AEDesc & outResult )
- {
- #pragma unused ( inAppleEvent, outAEReply, outResult )
-
- mPref->LoadPrefsData();
-
- // If dialog window is open, then setup the dialog according to the preferences.
- if ( mMainWindow != NULL )
- mPref->SetPrefsToWindow( mMainWindow );
- }
-
-
- // --------------------------------------------------
- // • HandleSavePrefs
- // --------------------------------------------------
-
- void
- CTouchMeApp::HandleSavePrefs(
- const AppleEvent & inAppleEvent,
- AppleEvent & outAEReply,
- AEDesc & outResult )
- {
- #pragma unused ( inAppleEvent, outAEReply, outResult )
-
- // If the main window is open, then read the settings from the dialog.
- if ( mMainWindow != NULL )
- mPref->GetPrefsFromWindow( mMainWindow );
-
- mPref->SavePrefsData();
- }
-
-
- // --------------------------------------------------
- // • HandleTouch
- // --------------------------------------------------
-
- void
- CTouchMeApp::HandleTouch(
- const AppleEvent & inAppleEvent,
- AppleEvent & outAEReply,
- AEDesc & outResult )
- {
- #pragma unused ( outAEReply, outResult )
-
- OSErr err;
-
- // Get the 1st required parameter as 'typeWildCard'.
- // Get the 1st required parameter as 'typeAEList'.
- // StAEDescriptor theDocListDesc;
- // theDocListDesc.GetParamDesc( inAppleEvent, keyDirectObject, typeAEList );
- StAEDescriptor theRequiredDesc;
- theRequiredDesc.GetParamDesc( inAppleEvent, keyDirectObject, typeWildCard );
- DescType theDirectObjectType;
- theDirectObjectType = theRequiredDesc.mDesc.descriptorType;
- switch ( theDirectObjectType ) {
- case typeAEList:
- case typeAlias:
- break;
- default:
- ThrowOSErr_( errAECoercionFail );
- break;
- }
-
- // Error if there are more parameters.
- UAppleEventsMgr::CheckForMissedParams( inAppleEvent );
-
-
- switch ( theDirectObjectType ) {
-
- // If the direct object is a list of files, then touch each file.
- case typeAEList:
- {
- long theCount;
- err = ::AECountItems( &(theRequiredDesc.mDesc), &theCount );
- ThrowIfOSErr_(err);
-
- for ( long index = 1; index <= theCount; index++ ) {
- AEKeyword theKey;
- DescType theType;
- FSSpec theFSSpec;
- Size theSize;
- err = ::AEGetNthPtr( &(theRequiredDesc.mDesc), index, typeFSS,
- &theKey, &theType, (Ptr) &theFSSpec, sizeof(FSSpec), &theSize );
- ThrowIfOSErr_(err);
-
- OpenDocument( &theFSSpec );
- }
- }
- break;
-
- // If the direct object is a single files, then touch it.
- case typeAlias:
- {
- FSSpec theFSSpec;
- UAppleEvents::TheFSSpec( theRequiredDesc.mDesc, theFSSpec );
-
- OpenDocument( &theFSSpec );
- }
- break;
- }
-
- }
-
-
- // --------------------------------------------------
- // • HandleFetch
- // --------------------------------------------------
-
- void
- CTouchMeApp::HandleFetch(
- const AppleEvent & inAppleEvent,
- AppleEvent & outAEReply,
- AEDesc & outResult )
- {
- #pragma unused ( outAEReply )
-
- OSErr err;
-
- // unsigned long theCreationDate, theModificationDate;
- // ::GetDateTime( &theCreationDate );
- // ::GetDateTime( &theModificationDate );
-
- // Get the 1st required parameter as 'typeWildCard'.
- // Get the 1st required parameter of 'keyDirectObject' as 'typeAEList'.
- // StAEDescriptor theDocListDesc;
- // theDocListDesc.GetParamDesc( inAppleEvent, keyDirectObject, typeAEList );
- StAEDescriptor theRequiredDesc;
- theRequiredDesc.GetParamDesc( inAppleEvent, keyDirectObject, typeWildCard );
- DescType theDirectObjectType;
- theDirectObjectType = theRequiredDesc.mDesc.descriptorType;
- switch ( theDirectObjectType ) {
- case typeAEList:
- case typeAlias:
- break;
- default:
- ThrowOSErr_( errAECoercionFail );
- break;
- }
-
- // Error if there are more parameters.
- UAppleEventsMgr::CheckForMissedParams( inAppleEvent );
-
-
- switch ( theDirectObjectType ) {
-
- // If the direct object is a list of files, then fetch the date time stamp of them.
- case typeAEList:
- {
- long theCount;
- err = ::AECountItems( &(theRequiredDesc.mDesc), &theCount );
- ThrowIfOSErr_(err);
-
- // The first item is significant. The rest ones will be ignored.
- for ( long index = 1; index <= 1 /*theCount*/; index++ ) {
- AEKeyword theKey;
- DescType theType;
- FSSpec theFSSpec;
- Size theSize;
- err = ::AEGetNthPtr( &(theRequiredDesc.mDesc), index, typeFSS,
- &theKey, &theType, (Ptr) &theFSSpec, sizeof(FSSpec), &theSize );
- ThrowIfOSErr_(err);
-
- // UFileTools::GetFSSpecDateTime( theFSSpec, theCreationDate, theModificationDate );
- HandleFetchDateTime( outResult, theFSSpec );
- }
- }
- break;
-
- // If the direct object is a single files, then fetch the date time stamp of it.
- case typeAlias:
- {
- FSSpec theFSSpec;
- UAppleEvents::TheFSSpec( theRequiredDesc.mDesc, theFSSpec );
-
- // UFileTools::GetFSSpecDateTime( theFSSpec, theCreationDate, theModificationDate );
- HandleFetchDateTime( outResult, theFSSpec );
-
- }
- break;
- }
-
- }
-
-
-
- // ==================================================
- // Common functions
- // ==================================================
-
- // --------------------------------------------------
- // • HandleGetPrefsSettings
- // --------------------------------------------------
-
- void
- CTouchMeApp::HandleGetPrefsSettings(
- AERecord & ioAERecord,
- ETouchType inTouchType,
- Boolean toCreateNewAERecord ) // true to dispose desc and create a new record [true]
- {
- OSErr err;
-
- // Dispose the original descriptor given by the system.
- // Instead, create my own descriptor, a record type with true option, otherwise a list type.
- if ( toCreateNewAERecord ) {
- ::AEDisposeDesc( &ioAERecord );
- ioAERecord.dataHandle = NULL;
- err = ::AECreateList( NULL, 0, true, &ioAERecord ); // Create as 'record'.
- ThrowIfOSErr_( err );
- }
-
- // If the main window is open, then read the settings from the dialog.
- if ( mMainWindow != NULL )
- mPref->GetPrefsFromWindow( mMainWindow );
-
- // Return the mPref data as an AppleEvent record.
- // 1. Enabled
- Boolean theEnabled = mPref->GetEnabled( inTouchType );
-
- // 2. Flag
- OSType theFlag = typeNull;
- switch ( (ETouchFlag) mPref->GetFlagNumb( inTouchType ) ) {
- case touchFlag_Current: theFlag = kEnumFlag_Current; break;
- case touchFlag_Direct: theFlag = kEnumFlag_Excact; break;
- case touchFlag_First: theFlag = kEnumFlag_First; break;
- case touchFlag_Second: theFlag = kEnumFlag_FirstSet; break;
- default: break;
- }
-
- // 3. Value
- LongDateTime theDateTimeSecs;
- longDateTimeHi( theDateTimeSecs ) = 0;
- longDateTimeLo( theDateTimeSecs ) = mPref->GetDateTime( inTouchType );
-
-
- // Prepare to create members of descriptor record.
- StAEDescriptor theDescEnabled( (Boolean) theEnabled );
- StAEDescriptor theDescFlag( typeEnumerated, &theFlag, sizeof(OSType) );
- StAEDescriptor theDescDate( typeLongDateTime, &theDateTimeSecs, sizeof(LongDateTime) );
-
- // Insert the members into to the descriptor record.
- UAEDesc::AddKeyDesc( &ioAERecord, pPref_Enabled, theDescEnabled.mDesc );
- UAEDesc::AddKeyDesc( &ioAERecord, pPref_Flag, theDescFlag.mDesc );
- UAEDesc::AddKeyDesc( &ioAERecord, pPref_Value, theDescDate.mDesc );
- }
-
-
- // --------------------------------------------------
- // • HandleSetPrefsSettings
- // --------------------------------------------------
-
- void
- CTouchMeApp::HandleSetPrefsSettings(
- AERecord & inAERecord,
- ETouchType inTouchType )
- {
- Boolean parameterWasFound = false;
-
- // 1. Enabled (Get the optional parameter 'pPref_Enabled' as 'typeBoolean')
- {
- Boolean theEnabled = mPref->GetEnabled( inTouchType );
- Boolean theNewEnabled;
-
- if ( UAppleEvents::GetParamBoolean( inAERecord, pPref_Enabled, theNewEnabled ) ) {
- if ( theEnabled != theNewEnabled ) {
- mPref->SetEnabled( inTouchType, theNewEnabled );
- parameterWasFound = true;
- }
- }
- }
-
-
- // 2. Flag (Get the optional parameter 'pPref_Flag' as 'typeEnumerated')
- {
- ETouchFlag theFlag = mPref->GetFlagNumb( inTouchType );
- OSType theAEFlag = typeNull;
-
- if ( UAppleEvents::GetParamEnum( inAERecord, pPref_Flag, theAEFlag ) ) {
- ETouchFlag theNewFlag = touchFlag_Null;
- switch ( (EnumTouchMe_Flag) theAEFlag ) {
- case kEnumFlag_Current: theNewFlag = touchFlag_Current; break;
- case kEnumFlag_Excact: theNewFlag = touchFlag_Direct; break;
- case kEnumFlag_First: theNewFlag = touchFlag_First; break;
- case kEnumFlag_FirstSet: theNewFlag = touchFlag_Second; break;
- default: break;
- }
-
- if ( theFlag != theNewFlag && theNewFlag != touchFlag_Null ) {
- mPref->SetFlagNumb( inTouchType, theNewFlag );
- parameterWasFound = true;
- }
-
- }
- }
-
- // 3. Value (Get the optional parameter 'pPref_Value' as 'typeBoolean')
- {
- LongDateTime theDateTimeSecs;
- longDateTimeHi( theDateTimeSecs ) = 0;
- longDateTimeLo( theDateTimeSecs ) = mPref->GetDateTime( inTouchType );
- LongDateTime theNewDateTimeSecs;
- if ( UAppleEvents::GetParamLongDateTime( inAERecord, pPref_Value, theNewDateTimeSecs ) ) {
- if ( longDateTimeHi(theDateTimeSecs) != longDateTimeHi(theNewDateTimeSecs) ||
- longDateTimeLo(theDateTimeSecs) != longDateTimeLo(theNewDateTimeSecs) ) {
- mPref->SetDateTime( inTouchType, longDateTimeLo( theNewDateTimeSecs ) );
- parameterWasFound = true;
- }
- }
- }
-
- if ( parameterWasFound && mMainWindow != NULL )
- mPref->SetPrefsToWindow( mMainWindow );
-
- }
-
-
- // --------------------------------------------------
- // • HandleFetchDateTime
- // --------------------------------------------------
-
- void
- CTouchMeApp::HandleFetchDateTime(
- AERecord & ioAERecord,
- FSSpec & inMacFSSpec,
- Boolean toCreateNewAERecord ) // = true
- {
- OSErr err;
-
- // Dispose the original descriptor given by the system.
- // Instead, create my own descriptor, a record type with true option, otherwise a list type.
- if ( toCreateNewAERecord ) {
- ::AEDisposeDesc( &ioAERecord );
- ioAERecord.dataHandle = NULL;
- err = ::AECreateList( NULL, 0, true, &ioAERecord ); // Create as 'record'.
- ThrowIfOSErr_( err );
- }
-
- unsigned long theCreationDate, theModificationDate;
- UFileTools::GetFSSpecDateTime( inMacFSSpec, theCreationDate, theModificationDate );
-
- mPref->SetDateTime( touchType_CreationDate, theCreationDate );
- mPref->SetDateTime( touchType_ModificationDate, theModificationDate );
-
- // If the main window is open, then setup the dialog according to the preferences.
- if ( mMainWindow != NULL ) mPref->SetPrefsToWindow( mMainWindow );
-
- LongDateTime theCrDateTimeSecs, theMdDateTimeSecs;
- longDateTimeHi( theCrDateTimeSecs ) = 0;
- longDateTimeLo( theCrDateTimeSecs ) = theCreationDate;
- longDateTimeHi( theMdDateTimeSecs ) = 0;
- longDateTimeLo( theMdDateTimeSecs ) = theModificationDate;
-
- // Prepare to create members of descriptor record.
- StAEDescriptor theCrDateDesc( typeLongDateTime, &theCrDateTimeSecs, sizeof(LongDateTime) );
- StAEDescriptor theMdDateDesc( typeLongDateTime, &theMdDateTimeSecs, sizeof(LongDateTime) );
-
- // Insert the members into to the descriptor record.
- UAEDesc::AddKeyDesc( &ioAERecord, pStamp_Creation, theCrDateDesc.mDesc );
- UAEDesc::AddKeyDesc( &ioAERecord, pStamp_Modification, theMdDateDesc.mDesc );
- }
-
-
- // end of program
-